﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;
using StackExchange.Redis;

namespace testRedis
{
    class Program
    {

        static void test1()
        {
            using (var redis = new RedisNativeClient("127.0.0.1", 6379, "crmwhz@71"))
            {
                redis.Set("中文键", Encoding.Default.GetBytes("中文测试值"));

                redis.Db = 1;
                redis.Set("中文键", Encoding.Default.GetBytes("中文测试值2"));
                Console.WriteLine("保存键值成功！");
            }

            using (var redis = new RedisNativeClient("127.0.0.1", 6379, "crmwhz@71"))
            {
                redis.Db = 1;
                byte[] data = redis.Get("中文键");
                StringBuilder sb = new StringBuilder();
                foreach (byte b in data)
                    sb.Append(b);
                Console.WriteLine("取得键值二进制:" + sb.ToString());
                String str = Encoding.Default.GetString(data);
                Console.WriteLine("取得键值字符串:" + str);
            }
        }

        static void test2()
        {

            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379,password=crmwhz@71");
            IDatabase db=redis.GetDatabase();

            byte[] key = Encoding.UTF8.GetBytes("中文键");
            db.StringSet("中文键", Encoding.UTF8.GetBytes("中文测试值"));
            Console.WriteLine("保存键值成功！");

            byte[] data = db.StringGet("中文键");
            StringBuilder sb = new StringBuilder();
            foreach (byte b in data)
                sb.Append(b);
            Console.WriteLine("取得键值二进制:" + sb.ToString());
            String str = Encoding.UTF8.GetString(data);
            Console.WriteLine("取得键值字符串:" + str);

            redis.Close();
        }

        static void Main(string[] args)
        {
            test1();
            Console.ReadKey();
        }
    }
}
